package com.thoughtworks.rhinoextension.javamodel;

import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;

import static junit.framework.Assert.assertEquals;

public class TestResultBuilderTest {

    @Test
    public void should_get_passes_failures() {
        TestResult result = objectLiteralToTestResult("{stats: {passes: 2, failures: 1, duration: 32}}");
        assertEquals(result.passes, 2);
        assertEquals(result.failures, 1);
        assertEquals(result.duration, 32);
    }

    @Test
    public void should_get_all_suites() {
        TestResult result = objectLiteralToTestResult("{stats: {passes: 2, failures: 1, duration: 32}, allSuites: [{}, {}]}");
        assertEquals(2, result.testSuites.size());
    }


    private TestResult objectLiteralToTestResult(String objectLiteral) {
        ScriptableObject scriptableObject = stringToJSObject(objectLiteral);
        TestResult result = new TestResultBuilder().build(scriptableObject);
        return result;
    }

    private ScriptableObject stringToJSObject(String objectLiteral) {
        Context context = Context.enter();
        ScriptableObject scope = context.initStandardObjects();
        ScriptableObject scriptableObject = (ScriptableObject) context.evaluateString(scope, "var a = " + objectLiteral + "; a", "", 1, null);
        return scriptableObject;
    }

}
